<?php
	/** Un Gestionnaire de statistiques par equipes
	 * @author sebastian mosser
	 * @since revision 61
	 */
	 
	 
class KOGoalStat {
	public $day = 0;
	public $aDate = "";
	public $minute = 0;
	public $location = 0;
	public $opponent = "";
	public $groundType = 0;
	public $ground;
	public $type = 0;
	//public function __construct() { } ;
}

class PlayerStat {
	public $matchesPlayed = 0;
	public $gameLength = 0;
	
	public $playAsPlayer = 0;
	public $playAsHelper = 0;
	
	public $redWarnings = 0;
	public $yellowWarnings = 0;
	
	public $goals = 0;
	public $goalsInside = 0;
	public $goalsOutside = 0;
	public $goalsNeutral = 0;
	
	public $helps = 0;
	public $helpsInside = 0;
	public $helpsOutside = 0;
	public $helpsNeutral = 0;
	
	public $minMark = 11;
	public $maxMark = 0;
	public $summedMark = 0;
	public $markCounter = 0;
	//public function __construct() {}
}

class TeamStat {

	public $gameLength = 0;
	
	public $goalsTotal = 0;
	public $goalsInside = 0;
	public $goalsOutside = 0;
	public $goalsNeutral = 0;
	
	public $koGoalsTotal = 0;
	public $koGoalsInside = 0;
	public $koGoalsOutside = 0;
	public $koGoalsNeutral = 0;
	
	public $helpsTotal = 0;
	public $helpsInside = 0;
	public $helpsOutside = 0;
	public $helpsNeutral = 0;
	
	public $matchesList = array();
	
	public function __construct() {}
	
}

class OKGoalStat {
	public $player;
	public $helper;
	public $minute;
	public $type;
	public $location;
	public $ground;
	public function __construct($m,$l) { 
		$this->minute = $m; $this->location = $l;
	}
}

class TeamStatMaker {

	// Données user
	private $type; // type de match a processer
	private $theTeam; // équipe a analyser
	
	// Données internes
	private $team;
	private $players;
	private $okGoals;
	private $koGoals;
	
	
	public static function process($team,$type) {

		$statMaker = new TeamStatMaker($team, $type);
		$statMaker->run();
		
		$result = array();
		$result["matcheList"]  = $statMaker->getMatchesList();
		
		$result["koGoals"]     = ""; //$statMaker->getKoGoals();
		$result["koGoalsSchedule"] = $statMaker->getKoGoalsSchedule();
		$result["koGoalsOverview"] = $statMaker->getKoGoalsOverview();
				
		$result["generalStat"] = $statMaker->getGeneralStat();
		
		$result["goalers"]     = $statMaker->getGoalers();
		$result["helpers"]     = $statMaker->getHelpers();
		
		$result["okGoals"]     = $statMaker->getOkGoals();
		$result["okGoalsOverview"] = $statMaker->getOkGoalsOverview();
		
		

		$ans = 
			self::toHtml($team->displayShort(),Matche::$MATCHE_TYPE[$type],$result);
		return $ans;
	}
	
	// On stat une equipe et un type de matche
	private function __construct($team, $type) {
		$this->type = $type;
		$this->theTeam = $team;
		$this->team = new TeamStat();
		$this->players = array();
		$this->okGoals = array();
		$this->koGoals = array();
	}


	/** Affichage des donnees **/	
	
	public function getOkGoals() {

		// On trie par tranche horaire.
		$tmp = array();
		foreach($this->okGoals as $k => $v) {
			$minute = ($v->minute < 1 ? 1 : ($v->minute - 1));
			
			$key = "". floor($minute / 10);
			if (! array_key_exists($key,$tmp))
				$tmp[$key] = array();
			$tmp[$key][] = $v;
		}
		
		ksort($tmp);
		// On calcule le résultat trié :
		$result = "";
		foreach($tmp as $k => $array) {
			$from = ($k*10) + 1;
			$to = $from + 9;
			
			$inside = 0; $outside = $neutral = 0; $total = 0;
			foreach($array as $v) {
				if ($v->ground == -1 or $v->ground == 3)
					$neutral += 1;
				else if ($v->ground == 1)
					$inside += 1;
				else
					$outside += 1;
			}
			$total = $inside + $outside + $neutral;
			$totalPercent = 
				$this->computePercent($total,$this->team->goalsTotal);
			
			$insidePercent = 
				$this->computePercent($inside,$this->team->goalsInside);
			$outsidePercent = 
				$this->computePercent($outside,$this->team->goalsOutside);
			$neutralPercent = 
				$this->computePercent($neutral,$this->team->goalsNeutral);
				
			$result .= <<<EOS
<tr> 
	<td> ${from}' </td> <td> à </td> <td> ${to}'</td>
	<th> ${total} </th> <td> ${totalPercent} </td>
	<th> ${inside}  </th> <td> ${insidePercent} </td>
	<th> ${outside} </th> <td> ${outsidePercent} </td>
	<th> ${neutral} </th> <td> ${neutralPercent} </td>

</tr>
EOS;
		}
		return str_replace(" 0 ","-",$result);
	}
	
	public function getOkGoalsOverview() {
		return $this->getGoalsOverview($this->okGoals,$this->team->goalsTotal);
	}
	
	public function getKoGoalsOverview() {
		return $this->getGoalsOverview($this->koGoals,$this->team->koGoalsTotal);
	}
	
	private function getGoalsOverview($goalsArray,$reference) {
	
		$total = $reference;
		$totalPercent = ($total > 0 ? 100 : 0);
			
		// Initialisation des tables de stockages
		$loc  = array()  ; $type = array();
		$comb = array()  ; $percent = array();
		$locPercent = array(); $typePercent = array();
		for ($i = 0; $i < 6; $i ++) {
			$type[$i] = 0;
			$typePercent[$i] = 0;
			for ($j = 0; $j < 4; $j++) {
				$loc[$j] = 0; $locPercent[$j] = 0;
				$comb["$i,$j"] = 0; $percent["$i,$j"] = 0;
			}
		}
	
		// Remplissage des tables de stockages loc, type et comb
		foreach($goalsArray as $v) {
			$loc[$v->location] += 1;
			$type[$v->type] += 1;
			$key = $v->type.",".$v->location;
			$comb[$key] += 1;
		}
	
		// Calcul des pourcentages pour les clés combinées
		foreach($comb as $k => $v) 
			$percent[$k] = $this->computePercent($v,$total);
		
		foreach($loc as $k => $v)
			$locPercent[$k] = $this->computePercent($v,$total);
		foreach($type as $k => $v)
			$typePercent[$k] = $this->computePercent($v,$total);
		
		$result = <<<EOS
<tr>
	<th> Type / Localisation  </th> <th colspan="2"> Surface de But </th> 
	<th colspan="2"> Surface de réparation </th> <th colspan="2"> En dehors </th>
	<th colspan="2"> Inconnue </th>	<th colspan="2"> Total </th>
</tr>
<tr> 
	<th> But </th> <th> Nombre </th> <th> % </th> 
	<th> Nombre </th> <th> % </th> <th> Nombre </th> <th> % </th>
	<th> Nombre </th> <th> % </th> <th> Nombre </th> <th> % </th>
</tr>
<tr>
	<th> Action de Jeu  </th> 
	<td> ${comb["1,1"]} </td> <th> ${percent["1,1"]} </th>
	<td> ${comb["1,2"]} </td> <th> ${percent["1,2"]} </th>
	<td> ${comb["1,3"]} </td> <th> ${percent["1,3"]} </th>
	<td> ${comb["1,0"]} </td> <th> ${percent["1,0"]} </th>
	<td> ${type[1]}     </td> <th> ${typePercent[1]} </th>
</tr>
<tr>
	<th> Coup Franc </th> 
	<td> ${comb["3,1"]} </td> <th> ${percent["3,1"]} </th>
	<td> ${comb["3,2"]} </td> <th> ${percent["3,2"]} </th>
	<td> ${comb["3,3"]} </td> <th> ${percent["3,3"]} </th>
	<td> ${comb["3,0"]} </td> <th> ${percent["3,0"]} </th>
	<td> ${type[3]}     </td> <th> ${typePercent[3]} </th>
</tr>
<tr>
	<th> Corner </th> 
	<td> ${comb["2,1"]} </td> <th> ${percent["2,1"]} </th>
	<td> ${comb["2,2"]} </td> <th> ${percent["2,2"]} </th>
	<td> ${comb["2,3"]} </td> <th> ${percent["2,3"]} </th>
	<td> ${comb["2,0"]} </td> <th> ${percent["2,0"]} </th>
	<td> ${type[2]}     </td> <th> ${typePercent[2]} </th>
</tr>
<tr>
	<th> Pénalty </th> 
	<td> ${comb["5,1"]} </td> <th> ${percent["5,1"]} </th>
	<td> ${comb["5,2"]} </td> <th> ${percent["5,2"]} </th>
	<td> ${comb["5,3"]} </td> <th> ${percent["5,3"]} </th>
	<td> ${comb["5,0"]} </td> <th> ${percent["5,0"]} </th>
	<td> ${type[5]}     </td> <th> ${typePercent[5]} </th>
</tr>
<tr>
	<th> Contre son camp </th> 
	<td> ${comb["4,1"]} </td> <th> ${percent["4,1"]} </th>
	<td> ${comb["4,2"]} </td> <th> ${percent["4,2"]} </th>
	<td> ${comb["4,3"]} </td> <th> ${percent["4,3"]} </th>
	<td> ${comb["4,0"]} </td> <th> ${percent["4,0"]} </th>
	<td> ${type[4]}     </td> <th> ${typePercent[4]} </th>
</tr>
<tr>
	<th> Inconnu </th> 
	<td> ${comb["0,1"]} </td> <th> ${percent["0,1"]} </th>
	<td> ${comb["0,2"]} </td> <th> ${percent["0,2"]} </th>
	<td> ${comb["0,3"]} </td> <th> ${percent["0,3"]} </th>
	<td> ${comb["0,0"]} </td> <th> ${percent["0,0"]} </th>
	<td> ${type[0]}     </td> <th> ${typePercent[0]} </th>
</tr>
<tr>
	<th> Total </th> 
	<td> ${loc[1]} </td> <th> ${locPercent[1]} </th>
	<td> ${loc[2]} </td> <th> ${locPercent[2]} </th>
	<td> ${loc[3]} </td> <th> ${locPercent[3]} </th>
	<td> ${loc[0]} </td> <th> ${locPercent[0]} </th>
	<td> ${total} </td> <th> ${totalPercent} </th>
</tr>
EOS;
		return str_replace(" 0 ","-",$result);
	}
	
	public function getOkGoalsLoc() {
		$values = array();
		foreach($this->okGoals as $k => $v) {
			if (! array_key_exists($v->location,$values))
				$values[$v->location] = 0;
			$values[$v->location] += 1;
		}

		$result = "";		
		foreach ($values as $k => $v) {
			$name = Matche::$GOAL_LOC[$k];
			$count = $v;
			$percent = $this->computePercent($count,$this->team->goalsTotal);
			$result .= <<<EOS
<tr>
	<td> ${name} </td> <td> ${count} </td> <td> ${percent} </td>
</tr>
EOS;
		}

		return $result;
	}
	
	public function getOkGoalsType() {
		$values = array();
		foreach($this->okGoals as $k => $v) {
			if (! array_key_exists($v->type,$values))
				$values[$v->type] = 0;
			$values[$v->type] += 1;
		}

		$result = "";		
		foreach ($values as $k => $v) {
			$name = Matche::$GOAL_TYPE[$k];
			$count = $v;
			$percent = $this->computePercent($count,$this->team->goalsTotal);
			$result .= <<<EOS
<tr>
	<td> ${name} </td> <td> ${count} </td> <td> ${percent} </td>
</tr>
EOS;
		}

		return $result;
	}
	
	public function getMatchesList() {
		$result = "";
		foreach ($this->team->matchesList as $k => $v)
			$result .= "<tr> $v </tr> \n";
		return $result;
	}
	
	public function getKoGoals() {
		$result = "";
		foreach($this->koGoals as $k => $v) {
			$day = $v->day; $date = Date::fromSql($v->aDate)->displaySquizzed();
			$minute = $v->minute; $from = Matche::$GOAL_LOC[$v->location];
			$op = $v->opponent; $ground = Matche::$GROUND_TYPE[$v->groundType];
			$type = Matche::$GOAL_TYPE[$v->type];
		
			$result .= <<<EOS
<tr>
	<td> ${day} </td> <td> ${date} </td> <td> ${minute}' </td>
	<td> ${type} </td>
	<td> ${from} </td> <td> ${ground} </td> <td> ${op} </td> 
</tr>
EOS;
		}
		return $result;
	}
	public function getKoGoalsSchedule() {
		// On trie par tranche horaire.
		$tmp = array();
		foreach($this->koGoals as $k => $v) {
			$minute = ($v->minute < 1 ? 1 : ($v->minute - 1));
			$key = "".floor($minute / 10);
			if (! array_key_exists($key,$tmp))
				$tmp[$key] = array();
			$tmp[$key][] = $v;
		}
		
		ksort($tmp);
		// On calcule le résultat trié :
		$result = "";
		//var_dump($this->team);
		//var_dump($tmp);
		foreach($tmp as $k => $array) {
			
			$from = ($k*10)+1;
			$to = $from + 9;
			
			$inside = 0; $outside = $neutral = 0; $total = 0;
			foreach($array as $v) {
				if ($v->ground == -1 or $v->ground == 3)
					$neutral += 1;
				else if ($v->ground == 1)
					$inside += 1;
				else
					$outside += 1;
			}
			$total = $inside + $outside + $neutral;

			$teamTotal = $this->team->koGoalsTotal;
			$teamInside = $this->team->koGoalsInside;
			$teamOutside = $this->team->koGoalsOutside;
			$teamNeutral = $this->team->koGoalsNeutral;

			$totalPercent = 
				$this->computePercent($total,$teamTotal);
			
			$insidePercent = 
				$this->computePercent($inside,$teamInside);
			$outsidePercent = 
				$this->computePercent($outside,$teamOutside);
			$neutralPercent = 
				$this->computePercent($neutral,$teamNeutral);
				
			$result .= <<<EOS
<tr> 
	<td> ${from}' </td> <td> à </td> <td> ${to}'</td>
	<th> ${total} </th> <td> ${totalPercent} </td>
	<th> ${inside}  </th> <td> ${insidePercent} </td>
	<th> ${outside} </th> <td> ${outsidePercent} </td>
	<th> ${neutral} </th> <td> ${neutralPercent} </td>

</tr>
EOS;
		}
		return str_replace(" 0 ","-",$result);
	}
	
		public function getKoGoalsLoc() {
		$values = array();
		foreach($this->koGoals as $k => $v) {
			if (! array_key_exists($v->location,$values))
				$values[$v->location] = 0;
			$values[$v->location] += 1;
		}

		$result = "";		
		foreach ($values as $k => $v) {
			$name = Matche::$GOAL_LOC[$k];
			$count = $v;
			$percent = 
				$this->computePercent($count,$this->team->koGoalsTotal);
			$result .= <<<EOS
<tr>
	<td> ${name} </td> <td> ${count} </td> <td> ${percent} </td>
</tr>
EOS;
		}

		return $result;
	}
	
	public function getKoGoalsType() {
		$values = array();
		foreach($this->koGoals as $k => $v) {
			if (! array_key_exists($v->type,$values))
				$values[$v->type] = 0;
			$values[$v->type] += 1;
		}

		$result = "";		
		foreach ($values as $k => $v) {
			$name = Matche::$GOAL_TYPE[$k];
			$count = $v;
			$percent = $this->computePercent($count,$this->team->koGoalsTotal);
			$result .= <<<EOS
<tr>
	<td> ${name} </td> <td> ${count} </td> <td> ${percent} </td>
</tr>
EOS;
		}
		return $result;
	}
	
	public function getGeneralStat() {
		
		$tmp = array();
		foreach ($this->players as $k => $v) {

			$name = Member::read($k)->displayName();
			$matchesPlayed = $v->matchesPlayed;
			$gameLength = $v->gameLength;
			
			$lengthPercent = 
					$this->computePercent($v->gameLength,$this->team->gameLength);
					
			$playAsPlayer = $v->playAsPlayer; $playAsHelper = $v->playAsHelper;
			$red = $v->redWarnings; 
			if ($red != 0)
				$red = "<span class=\"error\"> $red </span>";
			$yellow = $v->yellowWarnings;
			$goals = $v->goals; $helps = $v->helps;
			if ($matchesPlayed == 1) {
				$min = "-"; $max = "-";
			}
			else {
				$min = $v->minMark; $max = $v->maxMark;
				$min = ($min == 11 ? 0: $min);
			}
			if ($v->markCounter == 0)
				$average = 0;
			else {
				$average = ((float) $v->summedMark / $v->markCounter);
				$average = round($average,2);
			}
			

			$key = sprintf("%5d",$matchesPlayed) . "-" . $average . "-" . $name;
			
			if (array_key_exists($k,$this->theTeam->getPlayers()))
				$name = "<strong> $name </strong>";
				
			$tmp[$key] = <<<EOS
<tr>
	<td> ${name} </td> <th> ${matchesPlayed} </th> 
	<td> ${gameLength}' </td>
	<th> ${lengthPercent} </th> <td> ${playAsPlayer} </td> 
	<td> ${playAsHelper} </td> <td> ${yellow} </td> <td> ${red} </td>
	<td> ${goals} </td> <td> ${helps} </td> <td> ${min} </td> 
	<th> ${average} </th> <td> ${max} </td>
</tr>
EOS;
		}
		
		krsort($tmp);
		$result = "";
		foreach($tmp as $k => $v)
			$result .= $v;
		return str_replace(" 0 ","-",$result);
	}
	
	public function getGoalers() {
	
		$tmp = array();
		foreach($this->players as $k => $v) {
			if ($v->goals == 0)
				continue; 
			$name = Member::read($k)->displayName();

			$total = $v->goals;
			$totalPercent = $this->computePercent($total,$this->team->goalsTotal);

			$outside = $v->goalsOutside;
			$outsidePercent = 
				$this->computePercent($outside, $this->team->goalsOutside);
			
			$inside = $v->goalsInside;
			$insidePercent = 
				$this->computePercent($inside,$this->team->goalsInside);
			
			$neutral = $v->goalsNeutral;
			$neutralPercent = 
				$this->computePercent($neutral, $this->team->goalsNeutral);

			
			$key = sprintf("%5d",$total) . "-" . $name;
			
			if (array_key_exists($k,$this->theTeam->getPlayers()))
				$name = "<strong> $name </strong>";
			$tmp[$key] = <<<EOS
<tr>
	<td> ${name} </td> 
	<th> ${total}   </th> <td> ${totalPercent} </td>
	<th> ${outside} </th> <td> ${outsidePercent} </td>
	<th> ${inside}  </th> <td> ${insidePercent} </td>
	<th> ${neutral} </th> <td> ${neutralPercent} </td>
</tr>
EOS;
		
		}
		krsort($tmp);
		$result = "";
		foreach($tmp as $k=> $v)
			$result .= $v;

		return str_replace(" 0 ","-",$result);
	}
	
		public function getHelpers() {
	
		$tmp = array();
		foreach($this->players as $k => $v) {
			if ($v->helps == 0)
				continue; 
			$name = Member::read($k)->displayName();

			$total = $v->helps;
			$totalPercent = $this->computePercent($total,$this->team->helpsTotal);

			$outside = $v->helpsOutside;
			$outsidePercent = 
				$this->computePercent($outside, $this->team->helpsOutside);
			
			$inside = $v->helpsInside;
			$insidePercent = 
				$this->computePercent($inside,$this->team->helpsInside);
			
			$neutral = $v->helpsNeutral;
			$neutralPercent = 
				$this->computePercent($neutral, $this->team->helpsNeutral);

			
			$key = sprintf("%5d",$total) . "-" . $name;
			if (array_key_exists($k,$this->theTeam->getPlayers()))
				$name = "<strong> $name </strong>";
			$tmp[$key] = <<<EOS
<tr>
	<td> ${name} </td> 
	<th> ${total}   </th> <td> ${totalPercent} </td>
	<th> ${outside} </th> <td> ${outsidePercent} </td>
	<th> ${inside}  </th> <td> ${insidePercent} </td>
	<th> ${neutral} </th> <td> ${neutralPercent} </td>
</tr>
EOS;
		
		}
		krsort($tmp);
		$result = "";
		foreach($tmp as $k=> $v)
			$result .= $v;

		return str_replace(" 0 ","-",$result);
	}
	
	/** PARCOURS DES MATCHES POUR CALCULER LES DONNEES */
	
	private function run() {
		$matches = $this->theTeam->getMatches();
		foreach($matches as $k => $v) {
			try {
				$m = Matche::read($k);
				$this->processMatch($m);
			}catch (Exception $e) { continue; }
		}
	}
	
	private function checkPlayer($p) {
		if (! array_key_exists($p, $this->players))
			$this->players[$p] = new PlayerStat();
	}
	
	private function processMatch($matche) {
		if ($matche->getType() != $this->type)
			return; // rien a foutre
		
		// On process l'equipe 
		$this->team->matchesList[] = $matche->displayInline();
		$this->team->gameLength += $matche->getLength();
		
		// On passe sur les joueurs
		foreach ($matche->getPlayers() as $k => $v) 
			$this->processPlayer($k,$v);
			
		// On passe sur les cartons
		foreach($matche->getWarnings() as $k => $v)
			$this->processWarning($v);
		
		// On passe sur les buts
		foreach($matche->getGoals() as $k => $v)
			$this->processGoal($v,$matche);
	}
	
	private function processPlayer($p,$infoArray) {
		// On recuperer une structure de stat sur le joueur
		$this->checkPlayer($p);
		$infos = $this->players[$p];
		
		// on statue ...
		$infos->matchesPlayed += 1;
		$infos->gameLength += $infoArray["gameTime"];
		
		// Les notes
		if ($infoArray["mark"] != -1) {
			$infos->markCounter += 1;
			$infos->minMark = min($infoArray["mark"],$infos->minMark);
			$infos->maxMark = max($infoArray["mark"],$infos->maxMark);
			$infos->summedMark += $infoArray["mark"];
		}
		
		// Le role
		$role = $infoArray["role"];
		if ($role == -1 or $role == 6)
			$infos->playAsHelper += 1;
		else
			$infos->playAsPlayer += 1;
	}
	
	private function processWarning($warning) {
		// On recuperer une structure de stat sur le joueur
		$this->checkPlayer($warning["player"]);
		$infos = $this->players[$warning["player"]];
		
		// Recuperation de la couleur du carton :
		$color = WarningType::getColor($warning["type"]);
		if ($color == "R")
			$infos->redWarnings += 1;
		else
			$infos->yellowWarning += 1;
	}
	
	private function processGoal($infoArray,$matche) {
		if ($infoArray["forUs"] == Matche::$FOR_US)
			$this->processOkGoal($infoArray,$matche);
		else
			$this->processKoGoal($infoArray,$matche);
	}
	
	private function processOkGoal($infoArray,$matche) {
	
		$where = $matche->getGround();
		$aGoalStat = 
			new OkGoalStat($infoArray["minute"],$infoArray["localisation"]);
		$aGoalStat->player = $infoArray["player"];
		$aGoalStat->helper = $infoArray["helper"];
		$aGoalStat->type = $infoArray["type"];
		$aGoalStat->ground = $where;
		$this->okGoals[] = $aGoalStat;

		$this->team->goalsTotal += 1;		
		$p = $infoArray["player"];

		if ($p != -1) {
			$this->checkPlayer($p);
			$infos = $this->players[$p];
			$infos->goals += 1; // un but en plus

			if ($where == -1 or $where == 3) {// but neutre
				$infos->goalsNeutral += 1;
				$this->team->goalsNeutral += 1;
			}
			else if ($where == 1){ // but domicile
				$infos->goalsInside += 1;
				$this->team->goalsInside += 1;
			}
			else {
				$infos->goalsOutside += 1;
				$this->team->goalsOutside += 1;
			}
		}
		

		$h = $infoArray["helper"];
		if ($h != -1) {
			$this->team->helpsTotal += 1;
			$this->checkPlayer($h);
			$infos = $this->players[$h];
			$infos->helps += 1; // une passe en plus
			
			if ($where == -1 or $where == 3) { // passe en terrain neutre
				$infos->helpsNeutral += 1;
				$this->team->helpsNeutral += 1;
			}
			else if ($where == 1) { // passe domicile
				$infos->helpsInside += 1;
				$this->team->helpsInside += 1;
			}
			else {
				$infos->helpsOutside += 1;
				$this->team->helpsOutside += 1;
			}
		}
	}
	
	
	private function processKoGoal($infoArray,$matche) {
		
		$stat = new KOGoalStat();
		$stat->day = $matche->getDay();
		$stat->aDate = $matche->getDate();
		$stat->minute = $infoArray["minute"];
		$stat->location = $infoArray["localisation"];
		$stat->type = $infoArray["type"];
		$stat->opponent = $matche->getOpponent();
		$stat->groundType = $matche->getGroundType();
		$stat->ground = $matche->getGround();
		$this->koGoals[] = $stat;
		
		$where = $stat->ground;

		$this->team->koGoalsTotal += 1;
		if ($where == -1 or $where == 3) {// but neutre
			$this->team->koGoalsNeutral += 1;
		}
		else if ($where == 1){ // but domicile
			$this->team->koGoalsInside += 1;
		}
		else {
			$this->team->koGoalsOutside += 1;
		}
		
	}
	
	private function computePercent($num,$denom) {
		if ($denom == 0)
			return 0;
		$tmp = ((float) $num / $denom) * 100;
		return round($tmp,2);
	}
	
	
	private static function toHtml($teamName, $displayTitle,$resultSet) {
		$matches = "<h2> Matchs répondant aux critères </h2>";
    $matches .= "<div align=\"center\">";
		if ($resultSet["matcheList"] != "") {
			$matches .= <<<EOS
<table class="resultset">
	<tr>
		<th> Date </th> <th> Adversaire </th> 
		<th> Score Club </th> <th>Score Adv.</th>
	</tr>
${resultSet["matcheList"]}
</table>
EOS;
		} else {
			$matches .= <<<EOS
<span class="error"> Aucun des matches présents dans Balloon pour votre 
équipe ne correspond à votre demande ... </span> 
EOS;
		}
		$matches .= "</div>\n";
		
		$general = 
			"<h2 style=\"page-break-before:always\"> Statistiques générales </h2>";
		$general .= "<div align=\"center\">";
		if ($resultSet["generalStat"] != "") {
			$general .= <<<EOS
<table class="resultset">
	<tr>
		<th> Effectif </th> <th colspan="3"> Matches </th> 
		<th colspan="2"> Poste </th> <th colspan="2"> Cartons </th>
		<th colspan="2"> Buts </th> <th colspan="3"> Notation </th>
	</tr>
	<tr>
		<th> Joueur </th> <th> Nb. Joués </th> <th> Tps de Jeu </th>
		<th> % </th> <th> Tit. </th> <th> Rpl. </th> <th> J </th> <th> R </th>
		<th> Buteur </th> <th> Passeur </th> <th> Min </th> <th> Moy. </th> 
		<th> Max </th>
${resultSet["generalStat"]}
</table>
EOS;
		} else {
			$general .= <<<EOS
<span class="error"> Aucune donnée n'a pu être calculée ... </span>
EOS;
		}
		$general .= "</div>\n";
		
		$okGoals = "<h2 style=\"page-break-before:always\"> Buts Inscrits </h2>";

		$okGoals .= "<h3> Par plage horaire </h3>";
		$okGoals .= "<div align=\"center\">";
		if ($resultSet["okGoals"] != "") {
			$okGoals .= <<<EOS
<table class="resultset">
	<tr>
		<th colspan="3"> Horaire </th> <th colspan="2"> Total </th>
		<th colspan="2"> Domicile </th>
		<th colspan="2"> Exterieur </th> <th colspan="2"> Neutre </th>
		
	</tr>
	<tr>
		<th colspan="3"> Plage </th> <th> Nb. </th> <th> % </th> <th> Nb. </th> 
		<th> % </th> 
		<th> Nb. </th> <th> % </th> <th> Nb. </th> <th> % </th>
${resultSet["okGoals"]}
</table>
EOS;
		} else {
			$okGoals .= <<<EOS
<span class="error"> Aucune donnée n'a pu être calculée ... </span>
EOS;
		}
		$okGoals .= "</div>";
		
		$okGoals .= "<h3> Récapitulatif </h3>\n<div align=\"center\">";
		if ($resultSet["okGoalsOverview"] != "") {
			$okGoals .= <<<EOS
<table class="resultset">
${resultSet["okGoalsOverview"]}
</table>
EOS;
		} else {
			$okGoals .= <<<EOS
<span class="error"> Aucune donnée n'a pu être calculée ... </span>
EOS;
		}
		$okGoals .= "</div>";
		
		$koGoals = "<h2 style=\"page-break-before:always\"> Buts Encaissés </h2>";
/** $koGoals .= "<h3> Liste Récapitulative </h3>";
		$koGoals .= "<div align=\"center\">";
		if ($resultSet["koGoals"] != "") {
			$koGoals .= <<<EOS
<table class="resultset">
	<tr>
		<th> Journée </th> <th> Date </th>
		<th> Minute </th> <th> Type de but </th> <th> Localisation </th>
		<th> Type Terrain </th> <th> Adversaire </th>
	</tr>
${resultSet["koGoals"]}
</table>
EOS;
		} else { 
			$koGoals .= <<<EOS
<span class="error"> Aucune donnée n'a pu être calculée ... </span>
EOS;
		}
		$koGoals .= "</div>"; **/
		
		$koGoals .= "<h3> Par plage horaire </h3>";
		$koGoals .= "<div align=\"center\">";
		if ($resultSet["koGoalsSchedule"] != "") {
			$koGoals .= <<<EOS
<table class="resultset">
	<tr>
		<th colspan="3"> Horaire </th> <th colspan="2"> Total </th>
		<th colspan="2"> Domicile </th>
		<th colspan="2"> Exterieur </th> <th colspan="2"> Neutre </th>
		
	</tr>
	<tr>
		<th colspan="3"> Plage </th> <th> Nb. </th> <th> % </th> <th> Nb. </th> 
		<th> % </th> 
		<th> Nb. </th> <th> % </th> <th> Nb. </th> <th> % </th>
${resultSet["koGoalsSchedule"]}
</table>
EOS;
		} else {
			$koGoals .= <<<EOS
<span class="error"> Aucune donnée n'a pu être calculée ... </span>
EOS;
		}
		$koGoals .= "</div>";
		
		$koGoals .= "<h3> Récapitulatif </h3>\n<div align=\"center\">";
		if ($resultSet["koGoalsOverview"] != "") {
			$koGoals .= <<<EOS
<table class="resultset">
${resultSet["koGoalsOverview"]}
</table>
EOS;
		} else {
			$koGoals .= <<<EOS
<span class="error"> Aucune donnée n'a pu être calculée ... </span>
EOS;
		}
		$koGoals .= "</div>";
		
		
		$goalers = 
			"<h2 style=\"page-break-before:always\"> Classements des Buteurs </h2>";
		$goalers .= "<div align=\"center\">";
		if ($resultSet["goalers"] != "") {
			$goalers .= <<<EOS
<table class="resultset">
	<tr>
		<th> Effectif </th> <th colspan="2"> Total </th>
		<th colspan="2"> Exterieur </th> <th colspan="2"> Domicile </th>
		<th colspan="2"> Neutre </th>
	</tr>
	<tr>
		<th> Joueur </th> <th> Nb. </th> <th> % </th> <th> Nb. </th> <th> % </th> 
		<th> Nb. </th> <th> % </th> <th> Nb. </th> <th> % </th>
${resultSet["goalers"]}
</table>
EOS;
		} else {
			$goalers .= <<<EOS
<span class="error"> Aucune donnée n'a pu être calculée ... </span>
EOS;
		}
		$goalers .= "</div>";
		
		$helpers = 
			"<h2 style=\"page-break-before:always\"> Classement des Passeurs </h2>";
		$helpers .= "<div align=\"center\">";
		if ($resultSet["helpers"] != "") {
			$helpers .= <<<EOS
<table class="resultset">
	<tr>
		<th> Effectif </th> <th colspan="2"> Total </th>
		<th colspan="2"> Exterieur </th> <th colspan="2"> Domicile</th>
		<th colspan="2"> Neutre </th>
	</tr>
	<tr>
		<th> Joueur </th> <th> Nb. </th> <th> % </th> <th> Nb. </th> <th> % </th> 
		<th> Nb. </th> <th> % </th> <th> Nb. </th> <th> % </th> 
${resultSet["helpers"]}
</table>
EOS;
		
		} else {
			$helpers .= <<<EOS
<span class="error"> Aucune donnée n'a pu être calculée ... </span>
EOS;
		}
		$helpers .= "</div>";		
		
		$page = "<h1> $teamName : Statistiques ${displayTitle}</h1>";
		
		$page  .= <<<EOS

${matches}

${general}

${okGoals}

${koGoals}

${goalers}

${helpers}

EOS;
		return $page;
	}	
	
}

?>